package com.tao.com.xiaowen.config.download;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * Created by tao on 17-2-26.
 */

public class DownLoadImageHelper implements Runnable {
    private Context context;
    private String url;
    private ImageDownLoadCallBack callBack;
    private File currentFile;

    public DownLoadImageHelper(Context context, String url, ImageDownLoadCallBack callBack){
        this.context = context;
        this.url = url;
        this.callBack = callBack;
    }

    @Override
    public void run() {
        File file = null;
        Bitmap bitmap = null;
        try {
//            将Image的二进制文件下载到硬盘缓存当中 .diskCacheStrategy(DiskCacheStrategy.ALL)调用
//            file = Glide.with(context)
//                    .load(url)
//                    .downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
//                    .get();

            bitmap = Glide.with(context)
                    .load(url)
                    .asBitmap()
                    .into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
                    .get();
            if (bitmap != null){
                saveImageToGallery(context, bitmap);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
//            if (file!=null){
//                callBack.onDownLoadSuccess(file);
//            }else {
//                callBack.onDownLoadFailed();
//            }
            if (bitmap!=null&&currentFile.exists()){
                callBack.onDownLoadSuccess(bitmap);
            }else {
                callBack.onDownLoadFailed();
            }
        }

    }

    private void saveImageToGallery(Context context, Bitmap bitmap) {
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
        String fileName = "笑闻";
        File appDir = new File(file,fileName);
        if (!appDir.exists()){
            appDir.mkdirs();
        }
        fileName = System.currentTimeMillis()+".jpg";
        currentFile = new File(appDir,fileName);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(currentFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 其次把文件插入到系统图库
        //        try {
        //            MediaStore.Images.Media.insertImage(context.getContentResolver(),
        //                    currentFile.getAbsolutePath(), fileName, null);
        //        } catch (FileNotFoundException e) {
        //            e.printStackTrace();
        //        }

        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(currentFile.getPath()))));
    }

    public interface ImageDownLoadCallBack{
        void onDownLoadSuccess(File file);
        void onDownLoadSuccess(Bitmap bitmap);
        void onDownLoadFailed();
    }
}
